﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Basmus.Values.Web;
using ExampleWebApplication.Code;

namespace ExampleWebApplication
{
    public partial class CosmicBobyList : System.Web.UI.Page
    {
        private readonly CosmicBoby[] _items = new []
                                                  {
                                                      new CosmicBoby { Id = 1, Name = "Earth", Type = CosmicBodyType.Planet},
                                                      new CosmicBoby { Id = 2, Name = "Moon", Type = CosmicBodyType.Satellite},
                                                      new CosmicBoby { Id = 3, Name = "Mars", Type = CosmicBodyType.Planet},
                                                      new CosmicBoby { Id = 4, Name = "Sun", Type = CosmicBodyType.Star},
                                                      new CosmicBoby { Id = 5, Name = "Oberon", Type = CosmicBodyType.Satellite},
                                                      new CosmicBoby { Id = 6, Name = "Uranus", Type = CosmicBodyType.Planet},
                                                  };

        private readonly CosmicBodyType[] _types = Enum.GetValues(typeof(CosmicBodyType)).Cast<CosmicBodyType>().ToArray();

        protected void Page_Init(object sender, EventArgs e)
        {
            var queryModel = LinkUtil.WebLinkService.CreateModel<Models.CosmicBodyList>(this);

            var anyType = new
                            {
                                Name = "Any",
                                Href = LinkUtil.WebLinkService.GetUrl(new Models.CosmicBodyList())
                            };
            ItemTypesList.DataSource = _types.Select(x => new
                                                  {
                                                      Name = x.ToString(),
                                                      Href = LinkUtil.WebLinkService.GetUrl(new Models.CosmicBodyList { Type = x })
                                                  })
                                             .Concat(new[] { anyType });

            ItemsList.DataSource = _items.Where(x => queryModel.Type == null || x.Type == queryModel.Type)
                                         .Where(x => string.IsNullOrWhiteSpace(queryModel.NamePart) || x.Name.Contains(queryModel.NamePart.Trim()))
                                         .Where(x => !queryModel.Id.HasValue || x.Id == queryModel.Id);

            DataBind();
        }

        protected void OnSearch(object sender, EventArgs e)
        {
            int id;

            var model = new Models.CosmicBodyList
                            {
                                Id = Int32.TryParse(IdText.Text, out id) ? new int?(id) : null,
                                NamePart = NamePartText.Text
                            };

            var href = LinkUtil.WebLinkService.GetUrl(model);
            Response.Redirect(href);
        }
    }
}